#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2022/7/12 10:50
@File: mysql.py
"""
import jaydebeapi
from .basic import DatabaseUtil
# class MysqlUtil(DatabaseUtil):
#
#     def __init__(self,user,password,ip,port=3306,**kwargs):
#         self.DB = {}
#         self.CURSOR={}
#         self.dbkeywords={
#             'user':user,
#             'password':password,
#             'host':ip,
#             'port':port,
#             'autocommit':False,
#             'cursorclass':DictCursor
#         }
#
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         for cursor in self.CURSOR.values():
#             cursor.close()
#         for db in self.DB.values():
#             db.close()
#
#     def commit(self):
#         for db in self.DB.values():
#             db.commit()
#
#     def rollback(self):
#         for db in self.DB.values():
#             db.rollback()
#
#     @property
#     def db(self):
#         _id=threading.get_ident()
#         if _id not in self.DB:
#             self.DB[_id]=pymysql.connect(**self.dbkeywords)
#         return self.DB[_id]
#
#     def execsql(self,sql,autoCommit=True,fetch=True):
#         self.cursor.execute(sql)
#         result=[]
#         if sql.lower().startswith('select') and fetch:
#             result+=list(self.cursor.fetchall())
#         if autoCommit:
#             self.db.commit()
#         return result

class MysqlUtil(DatabaseUtil):

    def __init__(self, user, password, ip, port=3306, **kwargs):
        self.CURSOR = {}
        jclassname = 'com.mysql.cj.jdbc.Driver'
        url=f"jdbc:mysql://{ip}:{port}"
        driver_args=[user,password]
        jarFile=self.getJarFile()
        print(jarFile)
        self.db=jaydebeapi.connect(jclassname,url,driver_args,jarFile)
        self.db.jconn.setAutoCommit(False)


if __name__ == '__main__':
    # with MysqlUtil('tdsqlpcloud','fxq5@123','10.20.249.156',port=15001) as db:
    #     r=db.execsql(f"select * from hspbs.uam_company")
    #     print(r)
    # with MysqlUtil('root@hgglsyb0_76#obtest','QAZwsx123','obproxy.ops.openlabs.hundsun.com') as db:
    #     db.execsql("CREATE GLOBAL TEMPORARY table hspbs.\"__UAM_COMPANY_TEMP\" on commit delete rows as select * from hspbs.uam_company",autoCommit=False)
    with MysqlUtil('root', 'root', '10.20.46.217', port=33061) as db:
        print(db.execsql(f"delete from hsamlbd.amlbd_client where client_id > '111400000000' and client_id < '111400000020';"))